﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT.BusinessLogic;
using TMDT.BusinessObjects;

namespace TMDT.Web.UI.Controls
{
    public partial class ChungLoaiSanPhamControl : System.Web.UI.UserControl
    {
        public static Guid IDChungLoaiSanPham;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            mtvView.ActiveViewIndex = 0;

            txtTenChungLoaiInsert.Text = "";
            var service = new ChungLoaiSanPhamManager();
            rptChungLoaiSanPham.DataSource = service.GetChungLoaiSanPham();
            rptChungLoaiSanPham.DataBind();
        }

        protected void lbnInsert_Click(object sender, EventArgs e)
        {
            var service = new ChungLoaiSanPhamManager();
            var entity = new ChungLoaiSanPham
                             {
                                 IDChungLoai = Guid.NewGuid(),
                                 TenChungLoai = txtTenChungLoaiInsert.Text
                             };
            service.Add(entity);
            LoadData();
        }

        protected void lbnUpdate_Click(object sender, EventArgs e)
        {
            var service = new ChungLoaiSanPhamManager();
            var entity = service.GetChungLoaiSanPham(IDChungLoaiSanPham);

            entity.TenChungLoai = txtTenChungLoaiUpdate.Text;
            service.Update(entity);

            LoadData();
        }

        protected void lbnCancel_Click(object sender, EventArgs e)
        {
            mtvView.ActiveViewIndex = 0;
        }

        protected void ibnDelete_Click(object sender, ImageClickEventArgs e)
        {
            var id = ((ImageButton)sender).CommandArgument;
            var service = new ChungLoaiSanPhamManager();
            service.Remove(id);

            LoadData();
        }

        protected void ibnEdit_Click(object sender, ImageClickEventArgs e)
        {
            var id = ((ImageButton)sender).CommandArgument;
            var service = new ChungLoaiSanPhamManager();
            var entity = service.GetChungLoaiSanPham(id);

            IDChungLoaiSanPham = entity.IDChungLoai;
            txtTenChungLoaiUpdate.Text = entity.TenChungLoai;

            mtvView.ActiveViewIndex = 1;
        }
    }
}